import 'dart:ui';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_custom_bottom_tab_bar/eachtab.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_slidable/flutter_slidable.dart';
import 'package:panda/base/Constant.dart';
import 'package:panda/home/ShoudanChild.dart';

class ShouDanPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return ShouDanFragmentState();
  }
}

class ShouDanFragmentState extends State<ShouDanPage>
    with WidgetsBindingObserver, SingleTickerProviderStateMixin,AutomaticKeepAliveClientMixin {
  TabController _tabController;
  final SlidableController slidableController = SlidableController();

  EasyRefreshController _controller = new EasyRefreshController();
  List<bool> bools = new List();
  int currentStatus = 0;
  bool isSelectAll = false;
  var titles = ['待收单', '已收单'];

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed) {
      if (!ModalRoute.of(context).isCurrent) {
        return;
      }
    }
  }

  @override
  void initState() {
    super.initState();
    _tabController =
        new TabController(vsync: this, initialIndex: 0, length: titles.length);
    WidgetsBinding.instance.addObserver(this);
    bools..add(false)..add(false)..add(false)..add(false)..add(false);
  }

  @override
  Widget build(BuildContext context) {
    ScreenUtil.instance = ScreenUtil(width: 750, height: 1334)..init(context);
    return new Scaffold(
      // appBar: new AppBar(
      //   title: new Text(
      //     "购物车",
      //     style: TextStyle(
      //         fontSize: ScreenUtil.getInstance().setSp(BIG_FONT_SIZE),
      //         color: Colors.black),
      //   ),
      //   elevation: 0.2,
      //   backgroundColor: white,
      //   brightness: Brightness.light,
      //   centerTitle: true,
      //   actions: <Widget>[
      //     GestureDetector(
      //       child: Container(
      //         alignment: Alignment.center,
      //         padding: EdgeInsets.fromLTRB(0, 0, 13, 0),
      //         child: Text(
      //           currentStatus == 0 ? '编辑' : '完成',
      //           style: TextStyle(
      //               fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
      //               color: Colors.black54),
      //         ),
      //       ),
      //       onTap: () {
      //         if (currentStatus == 0) {
      //           currentStatus = 1;
      //         } else {
      //           currentStatus = 0;
      //         }
      //         setState(() {});
      //       },
      //     )
      //   ],
      // ),
      body: AnnotatedRegion<SystemUiOverlayStyle>(
        child: Column(
          children: <Widget>[
            Container(
              width: double.infinity,
              alignment: Alignment.center,
              color: white,
              child: new TabBar(
                isScrollable: true,
                controller: _tabController,
                indicatorColor: Colors.transparent,
                labelColor: mainColor,
                labelPadding: EdgeInsets.all(0),
                unselectedLabelColor: Colors.black,
                tabs: <Widget>[
                  EachTab(
                   
                    text: titles[0],
                    textStyle:
                        TextStyle(fontSize: ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                  ),
                  EachTab(
                   
                    text: titles[1],
                    textStyle: TextStyle(
                        fontSize:
                            ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE)),
                  ),
                ],
              ),
            ),
            Container(
              height: 1,
              width: double.infinity,
              color: f0Color,
            ),
            Expanded(
              child: TabBarView(controller: _tabController,
               physics: NeverScrollableScrollPhysics(), // 禁止滑动
               children: <Widget>[
                Center(
                    child: new ChildOfShoudanPage(
                  type: 1,
                )),
                Center(
                    child: ChildOfShoudanPage(
                  type: 2,
                )),
                // Center(child: AllOrderPage(type: 9,)),
              ]),
            )
            // Expanded(
            //   child: new EasyRefresh(
            //     controller: _controller,
            //     header: MaterialHeader(),
            //     footer: MaterialFooter(),
            //     enableControlFinishRefresh: true,
            //     child: ListView.builder(
            //       itemCount: bools.length,
            //       itemBuilder: (BuildContext context, int index) {
            //         return renderView(context, index);
            //       },
            //     ),
            //     onRefresh: () {},
            //   ),
            // ),
            // Container(
            //   height: 0.5,
            //   color: f0Color,
            // ),
            // Container(
            //   color: white,
            //   padding: EdgeInsets.fromLTRB(
            //       ScreenUtil.getInstance().setHeight(5),
            //       ScreenUtil.getInstance().setHeight(DP_10),
            //       ScreenUtil.getInstance().setHeight(DP_20),
            //       ScreenUtil.getInstance().setHeight(DP_10)),
            //   child: Row(
            //     children: <Widget>[
            //       CircularCheckBox(
            //           value: isSelectAll,
            //           activeColor: mainColor,
            //           inactiveColor: black_99,
            //           materialTapTargetSize: MaterialTapTargetSize.padded,
            //           onChanged: (bool x) {
            //             isSelectAll = !isSelectAll;
            //             checkAll();
            //             setState(() {});
            //           }),
            //       Text('全选',
            //           style: TextStyle(
            //               fontSize:
            //                   ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
            //               color: Colors.black87)),
            //       Expanded(
            //         child: Text(''),
            //       ),
            //       Text(currentStatus == 0 ? '合计：' : '',
            //           style: TextStyle(
            //               fontSize:
            //                   ScreenUtil.getInstance().setSp(MINI_FONT_SIZE),
            //               color: Colors.black87)),
            //       Text(currentStatus == 0 ? (MONEY + '159.98') : '',
            //           style: TextStyle(
            //               fontSize:
            //                   ScreenUtil.getInstance().setSp(NORMAL_FONT_SIZE),
            //               color: mainColor)),
            //       Container(
            //         width: 5,
            //       ),
            //       new Container(
            //         height: ScreenUtil.getInstance().setHeight(DP_60),
            //         decoration: new BoxDecoration(
            //             gradient: const LinearGradient(colors: [
            //               Color.fromARGB(255, 255, 140, 16),
            //               Color.fromARGB(255, 254, 99, 16)
            //             ]),
            //             borderRadius: BorderRadius.circular(20.0)),
            //         padding: EdgeInsets.fromLTRB(5.0, 0.0, 5.0, 0.0),
            //         child: new FlatButton(
            //           child: new Padding(
            //             padding: EdgeInsets.all(0.0),
            //             child: new Text(
            //               currentStatus == 0 ? "去结算(0)" : '删除',
            //               style: new TextStyle(
            //                   color: Colors.white,
            //                   fontSize: ScreenUtil.getInstance()
            //                       .setSp(NORMAL_FONT_SIZE)),
            //             ),
            //           ),
            //           onPressed: () {
            //             if (currentStatus == 0) {
            //               Navigator.of(context)
            //                   .push(new MaterialPageRoute(builder: (context) {
            //                 return new BuyGoodsPage(
            //                   goods_id: "1",
            //                   goods_img:
            //                       "https://dss0.baidu.com/73F1bjeh1BF3odCf/it/u=3672294480,3119857275&fm=85&s=B69DEC6CF9A4BF76547314850300E088",
            //                   goods_name: "test",
            //                   price: "4.44",
            //                   skuInfo: "null",
            //                   skuJson: "null",
            //                 );
            //               }));
            //             } else {}
            //           },
            //         ),
            //       )
            //     ],
            //   ),
            // ),
          ],
        ),
        value: SystemUiOverlayStyle.dark,
      ),
    );
  }

  Widget renderView(BuildContext context, int index) {
    return Container(
      margin: EdgeInsets.fromLTRB(
          ScreenUtil.getInstance().setHeight(DP_15),
          ScreenUtil.getInstance().setHeight(DP_15),
          ScreenUtil.getInstance().setHeight(DP_15),
          0),
      decoration: BoxDecoration(
          color: white,
          borderRadius:
              BorderRadius.circular(ScreenUtil.getInstance().setHeight(DP_10))),
      child: Slidable(
        actionPane: SlidableStrechActionPane(),
        controller: slidableController,
        actionExtentRatio: 0.25,
        secondaryActions: <Widget>[
          IconSlideAction(
            caption: '删除',
            color: mainColor,
            icon: Icons.delete,
            closeOnTap: true,
            onTap: () {},
          ),
        ],
        child: Padding(
          padding: EdgeInsets.fromLTRB(
              ScreenUtil.getInstance().setHeight(0),
              ScreenUtil.getInstance().setHeight(DP_20),
              ScreenUtil.getInstance().setHeight(DP_20),
              ScreenUtil.getInstance().setHeight(DP_20)),
          child: new Row(
            crossAxisAlignment: CrossAxisAlignment.center,
            mainAxisAlignment: MainAxisAlignment.start,
            children: <Widget>[
              Checkbox(
                  value: bools[index],
                  activeColor: mainColor,
                  checkColor: black_99,
                  materialTapTargetSize: MaterialTapTargetSize.padded,
                  onChanged: (bool x) {
                    bools[index] = !bools[index];
                    setState(() {});
                  }),
              ClipRRect(
                borderRadius: BorderRadius.circular(5),
                child: CachedNetworkImage(
                imageUrl:
                    'https://img14.360buyimg.com/n0/jfs/t1/42163/17/16172/246472/5d8dd42cE12fa785a/efbd7ce9dbaac1c1.jpg',
                    width: ScreenUtil.getInstance().setHeight(DP_150),
                    fit: BoxFit.fill,
                    height: ScreenUtil.getInstance().setHeight(DP_150)),
              ),
              Container(
                width: ScreenUtil.getInstance().setHeight(DP_15),
              ),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: <Widget>[
                    Text('【推广试饮 首瓶11元】贵州茅台镇酱香型白酒53度 高粱纯粮食高度原浆怀郎老酒 1瓶试饮装',
                        maxLines: 2,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(SMALL_FONT_SIZE),
                            color: Colors.black87)),
                    Container(
                      height: ScreenUtil.getInstance().setHeight(DP_10),
                    ),
                    Text('礼盒装：500ml',
                        maxLines: 1,
                        overflow: TextOverflow.ellipsis,
                        style: TextStyle(
                            fontSize:
                                ScreenUtil.getInstance().setSp(MINI_FONT_SIZE),
                            color: Colors.black38)),
                    Container(
                      height: ScreenUtil.getInstance().setHeight(DP_10),
                    ),
                    Row(
                      children: <Widget>[
                        Expanded(
                          child: Text(MONEY + '269.00',
                              maxLines: 1,
                              style: TextStyle(
                                  fontSize: ScreenUtil.getInstance()
                                      .setSp(BIG_FONT_SIZE),
                                  color: mainColor)),
                        ),
                        Icon(
                          Icons.remove,
                          color: black,
                          size: ScreenUtil.getInstance().setHeight(DP_30),
                        ),
                        Container(
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(2),
                              color: f0Color),
                          padding: EdgeInsets.fromLTRB(
                              ScreenUtil.getInstance().setHeight(DP_20),
                              2,
                              ScreenUtil.getInstance().setHeight(DP_20),
                              2),
                          margin: EdgeInsets.fromLTRB(
                              ScreenUtil.getInstance().setHeight(DP_10),
                              0,
                              ScreenUtil.getInstance().setHeight(DP_10),
                              0),
                          child: Text(
                            '1',
                            style: TextStyle(
                                fontSize: ScreenUtil.getInstance()
                                    .setSp(MINI_FONT_SIZE),
                                color: Colors.black87),
                          ),
                        ),
                        Icon(
                          Icons.add,
                          size: ScreenUtil.getInstance().setHeight(DP_30),
                          color: black,
                        )
                      ],
                    )
                  ],
                ),
              )
            ],
          ),
        ),
      ),
    );
  }

  checkAll() {
    for (int i = 0; i < bools.length; i++) {
      bools[i] = isSelectAll;
    }
    setState(() {});
  }

  @override
  void dispose() {
    super.dispose();
    WidgetsBinding.instance.removeObserver(this); //销毁观察者
  }

  @override
  bool shouldReclip(CustomClipper<Path> oldClipper) {
    return false;
  }

  @override
  // TODO: implement wantKeepAlive
  bool get wantKeepAlive => true;
}
